/*
 * $Id: autocli.h,v 1.2 Broadcom SDK $
 * 
 * $Copyright: Copyright 2011 Broadcom Corporation.
 * This program is the proprietary software of Broadcom Corporation
 * and/or its licensors, and may only be used, duplicated, modified
 * or distributed pursuant to the terms and conditions of a separate,
 * written license agreement executed between you and Broadcom
 * (an "Authorized License").  Except as set forth in an Authorized
 * License, Broadcom grants no license (express or implied), right
 * to use, or waiver of any kind with respect to the Software, and
 * Broadcom expressly reserves all rights in and to the Software
 * and all intellectual property rights therein.  IF YOU HAVE
 * NO AUTHORIZED LICENSE, THEN YOU HAVE NO RIGHT TO USE THIS SOFTWARE
 * IN ANY WAY, AND SHOULD IMMEDIATELY NOTIFY BROADCOM AND DISCONTINUE
 * ALL USE OF THE SOFTWARE.  
 *  
 * Except as expressly set forth in the Authorized License,
 *  
 * 1.     This program, including its structure, sequence and organization,
 * constitutes the valuable trade secrets of Broadcom, and you shall use
 * all reasonable efforts to protect the confidentiality thereof,
 * and to use this information only in connection with your use of
 * Broadcom integrated circuit products.
 *  
 * 2.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS
 * PROVIDED "AS IS" AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES,
 * REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY,
 * OR OTHERWISE, WITH RESPECT TO THE SOFTWARE.  BROADCOM SPECIFICALLY
 * DISCLAIMS ANY AND ALL IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY,
 * NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES,
 * ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
 * CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING
 * OUT OF USE OR PERFORMANCE OF THE SOFTWARE.
 * 
 * 3.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL
 * BROADCOM OR ITS LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL,
 * INCIDENTAL, SPECIAL, INDIRECT, OR EXEMPLARY DAMAGES WHATSOEVER
 * ARISING OUT OF OR IN ANY WAY RELATING TO YOUR USE OF OR INABILITY
 * TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF
 * THE AMOUNT ACTUALLY PAID FOR THE SOFTWARE ITSELF OR USD 1.00,
 * WHICHEVER IS GREATER. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING
 * ANY FAILURE OF ESSENTIAL PURPOSE OF ANY LIMITED REMEDY.$
 * 
 */

#ifndef __BCM_INT_AUTOCLI_H__
#define __BCM_INT_AUTOCLI_H__

#include <sdk_config.h>
#include <bcm/types.h>

/*
 * Sizes and configuration defines
 */
#ifndef AUTOCLI_MAX_FPARAMS
#define AUTOCLI_MAX_FPARAMS 32
#endif

#ifndef AUTOCLI_MAX_VARIABLE_NAME
#define AUTOCLI_MAX_VARIABLE_NAME 64
#endif

#ifndef AUTOCLI_MAX_VARIABLES
#define AUTOCLI_MAX_VARIABLES 1024
#endif


/******************************************************************************
 *
 * Autogen Description Structures
 *
 *****************************************************************************/

/*
 * Description of a named parameter or structure member. 
 */
typedef struct autocli_parameter_desc_s {
    const char* basetype; 
    const char* name; 
    int pcount; 
    int array; 
} autocli_parameter_desc_t; 

typedef struct autocli_fparams_s {
    void* rv; 
    void* args[AUTOCLI_MAX_FPARAMS]; 
} autocli_fparams_t; 

/*
 * Function description structure
 */

typedef int (*autocli_wrapper_f)(autocli_fparams_t* params); 

typedef struct autocli_function_s { 
    const char* name;
    autocli_wrapper_f wrapper; 
    autocli_parameter_desc_t returns; 
    autocli_parameter_desc_t* params; 
    int nparams; 
} autocli_function_t; 

/*
 * Structure description structure
 */
typedef struct autocli_struct_type_s {
    const char* name; 
    int size; 
    const autocli_parameter_desc_t* struct_members; 
    void* (*maddr)(void* p, int idx); 
} autocli_struct_type_t; 


/*
 * Enum value mapping structure
 */
typedef struct autocli_enum_map_s {
    const char* name; 
    int value; 
} autocli_enum_map_t; 

/*
 * Enum description structure
 */
typedef struct autocli_enum_type_s {
    const char* name; 
    autocli_enum_map_t* enum_map; 
} autocli_enum_type_t; 


/*
 * Autogenerated CLI Description Structure
 */
typedef struct autocli_data_s {
    
    autocli_function_t* functions; 
    autocli_struct_type_t* structures;
    autocli_enum_type_t* enums; 
    autocli_parameter_desc_t* typedefs; 

} autocli_data_t; 


/*****************************************************************************
 * 
 * Public Interface
 *
 ****************************************************************************/

/*
 * Error Conditions
 */
typedef enum autocli_error_e {
    
    AUTOCLI_E_NONE =             0,
    AUTOCLI_E_INTERNAL =        -1,
    AUTOCLI_E_MEMORY =          -2,
    AUTOCLI_E_PARAM =           -3, 
    AUTOCLI_E_EXISTS =          -4, 
    AUTOCLI_E_TYPE_NOT_FOUND =  -5, 
    AUTOCLI_E_BAD_VARIABLE =    -6, 
    AUTOCLI_E_BAD_TYPE =        -7, 
    AUTOCLI_E_NOT_FOUND =       -8,
    AUTOCLI_E_BAD_EXPRESSION =  -9,
    AUTOCLI_E_LAST =            -10
    
} autocli_error_t; 
    

/*
 * Atomic data types structure
 */
#define AUTOCLI_ATOMIC_TYPE_F_INT            0x1

typedef struct autocli_atomic_type_s {
    const char* name; 
    int size; 
    uint32 flags; 
    int (*print)(void* p); 
    int (*set)(void* p, const char* expr); 
} autocli_atomic_type_t; 


extern int autocli_init(autocli_atomic_type_t* atomics); 

extern int autocli_add_data(autocli_data_t* data); 

extern int autocli_validate_types(int print); 

extern int autocli_variable_print(const char* var); 

extern int autocli_variable_create(const char* var, const char* type); 

extern int autocli_variable_delete(const char* var); 

extern int autocli_variable_show(const char* var); 

extern int autocli_variable_set(const char* var, const char* expr); 

extern int autocli_function_call(const char* f, const char** params, void** rv); 

extern const char* autocli_error(int err); 



/*****************************************************************************
 *
 * The following data structures are private and used internally 
 *
 ****************************************************************************/

/*
 * Generic datatype description structure
 */
#define AUTOCLI_DATATYPE_F_ATOMIC 0x1
#define AUTOCLI_DATATYPE_F_STRUCT 0x2
#define AUTOCLI_DATATYPE_F_ENUM   0x4
#define AUTOCLI_DATATYPE_F_FUNC   0x8

typedef struct autocli_datatype_s {

    /* Flags for this datatype */
    uint32 flags; 

    /* 
     * The description of this datatype
     */ 
    autocli_parameter_desc_t desc; 
    
    /*
     * Pointer to the description of the basetype
     */
    union {
        autocli_atomic_type_t* ap; 
        autocli_struct_type_t* sp; 
        autocli_enum_type_t* ep; 
        autocli_function_t* fp; 
        void* p; 
    } basetype;  

    /*
     * Original type name. May be different from basetype name based on aliases or typedefs. 
     * Also used for temporary storage.
     */
    char type[AUTOCLI_MAX_VARIABLE_NAME]; 
    

} autocli_datatype_t; 


/*
 * Generic variable instance structure
 */

#define AUTOCLI_VARIABLE_F_AUTO   0x1
#define AUTOCLI_VARIABLE_F_SDATA  0x2

typedef struct autocli_variable_s {

    /* Variable Flags */
    uint32 flags; 

    /* The name of the variable */
    char name[AUTOCLI_MAX_VARIABLE_NAME]; 
    
    autocli_datatype_t dt; 
    
    void* data; 
    int size; 

} autocli_variable_t; 



#endif /* __BCM_INT_AUTOCLI_H__ */
